/**
 * @description trainModel Service
 */

 const { trainModel, orderModel } = require('../db/index')
 const Op = require('sequelize').Op
 
 /**
  * 新用户注册
  * @param {string} userid 用户id
  * @param {string} nikename 昵称
  * @param {string} city 城市
  * @param {number} isVip 是否VIP
  * @param {string} createtime 注册时间
  */
 async function addData(params) {
  console.log(params)
   const result = await trainModel.create(params)
   const data = result.dataValues
   return data
 }
 
 /**
  * 
  * @param {string} username 用户名 
  */
 async function getUserInfo(username) {
   // 查询条件
   const whereOption = {
    username
   }
   const result = await trainModel.findOne({
     where: whereOption
   })
   if (result === null) {
     return result
   }
   return result.dataValues
 }
 
 /**
  * 修改用户信息
  * @param {*} param0 要修改的用户信息
  * @param {*} param1 查询条件
  */
 async function updateData(
   params,
   id
 ) {
   // 拼接修改内容
   const updateObj = params
 
   // 拼接查询条件
   const whereOption = {}
   if (id) {
     whereOption.id = id
   }
 
   // 执行修改
   const result = await trainModel.update(updateObj, {
     where: whereOption
   })
   return result[0] > 0
 }
 
 /**
  * 删除用户
  * @param {string} userid 用户id 
  */
 async function deleteData(id) {
   const result = await trainModel.destroy({
     where: {
       id
     }
   })
   return result > 0
 }
 
 /**
  * 分页查询所有用户
  * @param {number} page 当前页码
  * @param {number} pageSize 每页条数
  */
 async function queryAll({ page = 1, pageSize = 10, name }) {
   const whereOption = {}

   if (name) {
     whereOption.name = {
       [Op.like]: `%${name}%`
     }
   }
 
   if (page < 1) {
     page = 1
   }
   const result = await trainModel.findAll({
     limit: pageSize,
     offset: (page - 1) * pageSize,
     where: whereOption
   })
   const total = await trainModel.findAll()
   return {
     page,
     pageSize,
     total: total.length || 0,
     data: result || []
   }
 }

 async function queryAllApp(params) {
  console.log(params)
  // 查询的start是起始点
  const rs1 = await trainModel.findAll({
    where: {
      startStation: params.start
    }
  })
  // 查询start不是起始点
  let rs2 = await trainModel.findAll()
  rs2 = rs2.filter(v => v.startStation !== params.start)
  let rs = []
  for (let item of rs1) {
    if (item.line.includes(params.end)) {
      rs.push(item)
    }
  }

  for (let item of rs2) {
    let line = JSON.parse(item.line)
    let indexStart = line.findIndex(v => v.station === params.start)
    let indexEnd = line.findIndex(v => v.station === params.end)
    if (indexStart > -1 && indexEnd > -1 && indexEnd > indexStart) {
      rs.push(item)
    }
  }

  for (let r of rs) {
    let wherePo = {
      ticketTime: params.date,
      trainName: r.name
    }
    console.log(wherePo)
    let orders = await orderModel.findAll({
      where: wherePo
    })
    orders = orders.filter(v => v.status === '已支付' || v.status === '已改签')
    if (orders.length) {
      console.log('ordersordersordersorders', orders)
      let seats = JSON.parse(r.seats)
      let orderdTicket0 = 0;
      let orderdTicket1 = 0;
      let orderdTicket2 = 0;
      let orderdTicket3 = 0;
      let orders0 = orders.filter(v => v.seatInfo === '无座')
      let orders1 = orders.filter(v => v.seatInfo === '硬座')
      let orders2 = orders.filter(v => v.seatInfo === '硬卧')
      let orders3 = orders.filter(v => v.seatInfo === '软卧')
      orderdTicket0 = orders0.reduce((a, b) => {
        return a + JSON.parse(b.passangers).length
      }, 0)
      orderdTicket1 = orders1.reduce((a, b) => {
        return a + JSON.parse(b.passangers).length
      }, 0)
      orderdTicket2 = orders2.reduce((a, b) => {
        return a + JSON.parse(b.passangers).length
      }, 0)
      orderdTicket3 = orders3.reduce((a, b) => {
        return a + JSON.parse(b.passangers).length
      }, 0)
      seats[0].cnt = seats[0].cnt - orderdTicket0
      seats[1].cnt = seats[1].cnt - orderdTicket1
      seats[2].cnt = seats[2].cnt - orderdTicket2
      seats[3].cnt = seats[3].cnt - orderdTicket3
      r.seats = seats
      r.seats = JSON.stringify(r.seats)

    }
  }
  //  处理当天车票的余量
  return {
    data: rs
  }
 }
 
 module.exports = {
  addData,
   getUserInfo,
   updateData,
   deleteData,
   queryAll,
   queryAllApp
 }
 